home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / northc / example2.lzh / Script / script.c < prev    next >
C/C++ Source or Header  |  1990-09-09  |  2KB  |  109 lines

  1. /*  (c) 1990 S.Hawtin.
  2.   Permission is granted to copy this file provided
  3.    1) It is not used for commercial gain
  4.    2) This notice is included in all copies
  5.    3) Altered copies are marked as such
  6.  
  7.   No liability is accepted for the contents of the file.
  8.  
  9. */
  10.  
  11. #include <stdio.h>
  12.  
  13. #define BUF_LEN 256
  14.  
  15. int 
  16. copy(src,dest)
  17.     char *src;
  18.     char *dest;
  19.    {/* Copy a file from one place to another */
  20.     FILE *in;
  21.     FILE *out;
  22.     char buffer[BUF_LEN];
  23.     int count,total;
  24.  
  25.     in = fopen(src,"r");
  26.     out = fopen(dest,"w");
  27.     if(in==NULL || out==NULL)
  28.        {if(in!=NULL)
  29.             fclose(in);
  30.           else
  31.             printf("Failed to open \"%s\" for reading\n",src);
  32.         if(out!=NULL)
  33.             fclose(out);
  34.           else
  35.             printf("Failed to open \"%s\" for writing\n",dest);
  36.         return(-1);
  37.         }
  38.     count = 1;
  39.     total = 0;
  40.     while(count>0)
  41.        {
  42.         count = fread(buffer,sizeof(char),BUF_LEN,in);
  43.         if(count>0)
  44.            {total += count;
  45.             fwrite(buffer,sizeof(char),count,out);
  46.             }
  47.         }
  48.     fclose(in);
  49.     fclose(out);
  50.     return(total);
  51.     }
  52.  
  53. int 
  54. ask(str)
  55.     char *str;
  56.    {/* Ask a question and return the result */
  57.     int answer = -1;
  58.     char result[20];
  59.  
  60.     while(answer==-1)
  61.        {puts(str);
  62.         fflush(stdout);
  63.         gets(result);
  64.         switch (result[0])
  65.            {case 'y': case 'Y':
  66.                 answer = 1;
  67.                 break;
  68.             case 'n': case 'N':
  69.                 answer = 0;
  70.             }
  71.         }
  72.     return(answer ? ~0 : 0);
  73.     }
  74.  
  75. int 
  76. type(name)
  77.     char *name;
  78.    {/* Type out the contents of a file */
  79.     FILE *in;
  80.     char buffer[BUF_LEN];
  81.     int count;
  82.  
  83.     in = fopen(name,"r");
  84.     if(in==NULL)
  85.        {
  86.         printf("Failed to open \"%s\" for reading\n",name);
  87.         return(-1);
  88.         }
  89.     count = 1;
  90.     while(count>0)
  91.        {
  92.         count = fread(buffer,sizeof(char),BUF_LEN,in);
  93.         if(count>0)
  94.            {
  95.             fwrite(buffer,sizeof(char),count,stdout);
  96.             }
  97.         }
  98.     fclose(in);
  99.     return(0);
  100.     }
  101.  
  102. void 
  103. echo(str)
  104.     char *str;
  105.    {/* Put a string on the terminal */
  106.     puts(str);
  107.     putchar('\n');
  108.     }
  109.